#include "stdafx.h"
#include "Utils.h"

void WMReaderUtils::FileTimeToQString(const FILETIME &time, QString &timeString)
{
    SYSTEMTIME sysTime;
    memset(&sysTime, 0, sizeof(sysTime)); 

    if(FileTimeToSystemTime(&time, &sysTime)) {
        if(sysTime.wDay != 1 && sysTime.wMonth != 1 && sysTime.wYear != 1601) {

               timeString = QString("%1/%2/%3 %4:").arg(sysTime.wDay)
                                                   .arg(sysTime.wMonth)
                                                   .arg(sysTime.wYear)
                                                   .arg(sysTime.wHour);
               if(sysTime.wMinute < 10)
                   timeString.append(QString("0%1").arg(sysTime.wMinute));
               else 
                   timeString.append(QString("%1").arg(sysTime.wMinute));  
     
        } else {
            timeString.insert(0, tr("Not Available"));  
        }
    } else {
        timeString.insert(0, tr("Not Available")); 
    }
}

void WMReaderUtils::PathFromTreeItem(QTreeWidgetItem *item, QString &path)
{
    if(item->parent() == NULL) {
        path.insert(0, "\\*");
    } else {
        QTreeWidgetItem *curItem = item;
        path.prepend("\\*");

        while(curItem->parent() != NULL) {
            path.prepend(curItem->text(0));
            path.prepend("\\"); 
            curItem = curItem->parent();
        }         
    }
}

void WMReaderUtils::RegKeyFromTreeItem(QTreeWidgetItem *item, QString &regKey)
{
    QTreeWidgetItem *curItem = item;

    while(curItem->parent() != NULL) {
        regKey.prepend("\\");
        regKey.prepend(curItem->text(0));
        curItem = curItem->parent();
    } 
    regKey.truncate(regKey.length() - 1);
}

void WMReaderUtils::ProcTypeToString(DWORD procType, QString &procString)
{
    switch(procType) {
        case PROCESSOR_MIPS_R4000:
            procString = "MIPS R4000";
            break;
        case PROCESSOR_HITACHI_SH3:
            procString = "Hitachi SH3";
            break;
        case PROCESSOR_HITACHI_SH3E:
            procString = "Hitachi SH3E";
            break;
        case PROCESSOR_HITACHI_SH4:
            procString = "Hitachi SH4";
            break;
        case PROCESSOR_MOTOROLA_821:
            procString = "Motorola 821";
            break;
        case PROCESSOR_SHx_SH3:
            procString = "SH3";
            break;
        case PROCESSOR_SHx_SH4:
            procString = "SH4";
            break;
        case PROCESSOR_STRONGARM:
            procString = "StrongARM";
            break;
        case PROCESSOR_ARM720:
            procString = "ARM 720";
            break;
        case PROCESSOR_ARM820:
            procString = "ARM 820";
            break;
        case PROCESSOR_ARM920:
            procString = "ARM 920";
            break;
        case PROCESSOR_ARM_7TDMI:
            procString = "ARM 7 TDMI";
            break;
        default:
            procString = "Unknown type";
            break;
    }
}

double WMReaderUtils::BytesToMbs(DWORD bytes)
{
    double mbytes = 0, kbytes = 0;
    
    kbytes = static_cast<double>(bytes / 1024);
    mbytes = static_cast<double>(kbytes / 1024);

    return mbytes;
}